/* Routine to solve sevens puzzle in Jewels of the Oracle */
/* 
Puzzle is organized as a pyamid as follows:
      1  2  3  4  5  6  7 
1     xx xx xx __ xx xx xx
2     xx xx __ __ __ xx xx
3     xx __ __ __ __ __ xx
4     __ __ __ __ __ __ __
5     __ __ xx xx xx __ __

Each row must contain an integer product of 7 and another number
each column contains an integer quotient of 7.  Numbers are entered
into the spaces from a set of integers 0-9 of which there are two
of each integer. (ie two 0's, two 1's, etc.)

To do this requires the permutation of the 20 digits taken 20 at a time, some 10^18 possibilities.
Therefore, the routine will be set to run until a single solution is found.  The basic approach will
be to form a random permutation of the digits, fill the pyramid positions with the digits and check
the pyramid to see if a solution was found.

*/

/* find combinations of integers and check */
/* for products and quotients */
call RxFuncAdd 'SysLoadFuncs', 'f:\os2\dll\RexxUtil.dll', 'SysLoadFuncs'
call SysLoadFuncs
call SysCls
say 'Routine to find solution to "Jewels of the Oracle" puzzle'
say 'Panditah of the Seventh Mountain'
call syscurpos 12, 36
call syscurstate 'off'
say 'Working '
rotate = 0
/* digit = random( 0, 9, 123456789) */
trynum = 1
do until PuzzleSolved
   PuzzleSolved = 1
   /* find random sequence of digits and insure no digit is repeated more than twice */
   call setcounts
   sequence = ''
   count = 0
   do until count = 10
      digit = random(0,9)
      if cnt.digit>0 then do  /* we found a digit */
         sequence = sequence||digit
         cnt.digit=cnt.digit-1
         count = count+1
      end
   end /* do */
   if rotate//4=0 then text='\'
   if rotate//4=1 then text='' 
   if rotate//4=2 then text='/'
   if rotate//4=3 then text='|'
   call syscurpos 12, 44
   call charout , text
   rotate = rotate + 1
   if rotate = 100 then rotate = 0
   /* fill pyramid with digits from sequence */
   pyramid.1.4=7
   pyramid.2.3=substr(sequence,1,1)
   pyramid.2.4=substr(sequence,2,1)
   pyramid.2.5=substr(sequence,3,1)
   pyramid.3.2=substr(sequence,4,1)
   pyramid.3.3=0
   pyramid.3.4=substr(sequence,5,1)
   pyramid.3.5=substr(sequence,6,1)
   pyramid.3.6=substr(sequence,7,1)
   pyramid.4.1=5
   pyramid.4.2=0
   pyramid.4.3=3
   pyramid.4.4=substr(sequence,8,1)
   pyramid.4.5=substr(sequence,9,1)
   pyramid.4.6=substr(sequence,10,1)
   pyramid.4.7=8
   pyramid.5.1=6
   pyramid.5.2=3
   pyramid.5.6=8
   pyramid.5.7=4

   /* display present trial */
   call syscurpos 13, 29
   say 'Trying solution number: 'trynum
   call syscurpos 14, 37
   say '   '||pyramid.1.4
   call syscurpos 15, 37
   say '  '||pyramid.2.3||pyramid.2.4||pyramid.2.5
   call syscurpos 16, 37
   say ' '||pyramid.3.2||pyramid.3.3||pyramid.3.4||pyramid.3.5||pyramid.3.6
   call syscurpos 17, 37
   say pyramid.4.1||pyramid.4.2||pyramid.4.3||pyramid.4.4||pyramid.4.5||pyramid.4.6||pyramid.4.7
   call syscurpos 18, 37
   say pyramid.5.1||pyramid.5.2||'   '||pyramid.5.6||pyramid.5.7
   trynum=trynum+1
   /* test pyramid values to see if they are even multiples of 7 */
   /* first form row and column numbers */
   row1  =                                        pyramid.1.4
   row2  =                           pyramid.2.3||pyramid.2.4||pyramid.2.5
   row3  =              pyramid.3.2||pyramid.3.3||pyramid.3.4||pyramid.3.5||pyramid.3.6
   row4  = pyramid.4.1||pyramid.4.2||pyramid.4.3||pyramid.4.4||pyramid.4.5||pyramid.4.6||pyramid.4.7
   row5a = pyramid.5.1||pyramid.5.2;                               row5b =  pyramid.5.6||pyramid.5.7

   col1 =                                        pyramid.4.1||pyramid.5.1
   col2 =                           pyramid.3.2||pyramid.4.2||pyramid.5.2
   col3 =              pyramid.2.3||pyramid.3.3||pyramid.4.3
   col4 = pyramid.1.4||pyramid.2.4||pyramid.3.4||pyramid.4.4
   col5 =              pyramid.2.5||pyramid.3.5||pyramid.4.5
   col6 =                           pyramid.3.6||pyramid.4.6||pyramid.5.6
   col7 =                                        pyramid.4.7||pyramid.5.7

   /* then test each number to see if evenly divisible by 7 */
   if row1  // 7 <> 0 then PuzzleSolved = 0
   if row2  // 7 <> 0 then PuzzleSolved = 0
   if row3  // 7 <> 0 then PuzzleSolved = 0
   if row4  // 7 <> 0 then PuzzleSolved = 0
   if row5a // 7 <> 0 then PuzzleSolved = 0
   if row5b // 7 <> 0 then PuzzleSolved = 0
   if col1  // 7 <> 0 then PuzzleSolved = 0
   if col2  // 7 <> 0 then PuzzleSolved = 0
   if col3  // 7 <> 0 then PuzzleSolved = 0
   if col4  // 7 <> 0 then PuzzleSolved = 0
   if col5  // 7 <> 0 then PuzzleSolved = 0
   if col6  // 7 <> 0 then PuzzleSolved = 0
   if col7  // 7 <> 0 then PuzzleSolved = 0 
end /* do */
call syscurpos 13, 24
say 'Puzzle solved - pyramid values are:'
   call syscurpos 14, 37
say '   '||pyramid.1.4
   call syscurpos 15, 37
say '  '||pyramid.2.3||pyramid.2.4||pyramid.2.5
   call syscurpos 16, 37
say ' '||pyramid.3.2||pyramid.3.3||pyramid.3.4||pyramid.3.5||pyramid.3.6
   call syscurpos 17, 37
say pyramid.4.1||pyramid.4.2||pyramid.4.3||pyramid.4.4||pyramid.4.5||pyramid.4.6||pyramid.4.7
   call syscurpos 18, 37
say pyramid.5.1||pyramid.5.2||'   '||pyramid.5.6||pyramid.5.7
exit
setcounts:
   cnt.0=0;cnt.1=2;cnt.2=2;cnt.3=0;cnt.4=1
   cnt.5=1;cnt.6=1;cnt.7=1;cnt.8=0;cnt.9=2
return
